package com.kancy.jemeter.functions;

import com.kancy.jemeter.util.IDUtil;
import com.kancy.jemeter.util.MessageUtil;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.LinkedList;
import java.util.List;

/**
 * @author kancy
 * @date 2019/6/29 17:17
 */
public class SnowflakeIdFunction extends JemeterAbstractFunction {


	public SnowflakeIdFunction() {
		super(0,2);
	}

	@Override
	public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
		return setJMeterVariable(1, getResult());
	}


	private static final List<String> argumentDesclist = new LinkedList();

	static {
		argumentDesclist.add(MessageUtil.getResString("__snowflakeid.prefix.message"));
		argumentDesclist.add(MessageUtil.getResString("varname.message"));
	}

	@Override
	public List<String> getArgumentDesc() {
		return argumentDesclist;
	}

	public String getResult() {
		if(hasParameter(0)){
			String prefix = getParameter(0);
			return IDUtil.getSnowflakeNo(prefix);
		}
		return String.valueOf(IDUtil.getSnowflakeId());
	}

}
